package cn.wk.servlet;

import cn.wk.entity.Admins;
import cn.wk.services.AdminsServices;
import cn.wk.services.impl.AdminsServicesImpl;
import cn.wk.util.JsonRes;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/adminsadd")
public class Servletadminsadd extends HttpServlet {
    private static  AdminsServices as=new AdminsServicesImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //1. 接收之前转换
           request.setCharacterEncoding("utf-8");
           //2. 接收
        Map<String, String[]> map = request.getParameterMap();
        String s = request.getParameter("status");

        Admins admin=new Admins();

        try {
            BeanUtils.populate(admin,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        int b=0;
        if(s!=null){
            admin.setStatus(1);
        }else {
            admin.setStatus(0);
        }
        if(admin!=null){
            if(admin.getAid()>0){
                b= as.MidffAdmins(admin.getApwd(),admin.getAname(),admin.getAgender(),
                        admin.getAddress(),admin.getAtel(),admin.getAemail(),admin.getStatus(),admin.getAid());
            }else{
                b= as.AddAdmins(admin.getAno(),admin.getApwd(),admin.getAname(),
                        admin.getAgender(),admin.getAddress(),admin.getAtel(),
                        admin.getAemail(),admin.getStatus());
            }
        }
        if(b>0)
            response.getWriter().println(JsonRes.res());
        else
            response.getWriter().println(JsonRes.error());

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
